package modeleditor.editor;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.geom.Ellipse2D;
import java.net.URL;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import linechart.Linechart;
import linechart.Line;
import linechart.Point;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RefineryUtilities;

/**
 * A simple demonstration application showing how to create a line chart using 
 * data from a {@link CategoryDataset}.
 */

public class drawlinechart {
	    /**
	     * Creates a new demo.
	     *
	     * @param title  the frame title.
	     */
		//private static CategoryDataset dataset;
		private static Linechart linechart;
		
	    public drawlinechart(Linechart linechart) {
	        //super(title);
	        //JPanel chartPanel = createDemoPanel();
	        //chartPanel.setPreferredSize(new Dimension(500, 270));
	        //setContentPane(chartPanel);
	    	this.linechart = linechart;
	    }

	    /**
	     * Creates a sample dataset.
	     * 
	     * @return The dataset.
	     */
	    public static CategoryDataset createDataset() {
	        //DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	    	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	        
	        for(int i=0; i< linechart.getLines().size(); i++){
	        	for(int j=0; j< ((Line) linechart.getLines().get(i)).getPoints().size(); j++){
	        		dataset.addValue(((Point) ((Line) linechart.getLines().get(i)).getPoints().get(j)).getValue(), ((Line) linechart.getLines().get(i)).getLname(), ((Point) ((Line) linechart.getLines().get(i)).getPoints().get(j)).getColumnkey());
	        	}
	        }
	        
	        return dataset;
	    }
	    
	    /**
	     * Creates a sample chart.
	     * 
	     * @param dataset  a dataset.
	     * 
	     * @return The chart.
	     */
	    public static JFreeChart createChart(CategoryDataset dataset) {
	        
	        // create the chart...
	        JFreeChart chart = ChartFactory.createLineChart(
	            linechart.getChartname(),   // chart title
	            linechart.getXname(),                       // domain axis label
	            linechart.getYname(),                   // range axis label
	            dataset,                         // data
	            PlotOrientation.VERTICAL,        // orientation
	            true,                           // include legend
	            true,                            // tooltips
	            false                            // urls
	        );
	        /*
	        chart.addSubtitle(new TextTitle("Number of Classes By Release"));
	        TextTitle source = new TextTitle(
	            "Source: Java In A Nutshell (5th Edition) " 
	            + "by David Flanagan (O'Reilly)"
	        );	        
	        source.setFont(new Font("SansSerif", Font.PLAIN, 10));
	        source.setPosition(RectangleEdge.BOTTOM);
	        source.setHorizontalAlignment(HorizontalAlignment.RIGHT);
	        chart.addSubtitle(source);
	        */
	        chart.setBackgroundPaint(Color.white);

	        CategoryPlot plot = (CategoryPlot) chart.getPlot();
	        plot.setBackgroundPaint(Color.lightGray);
	        plot.setRangeGridlinesVisible(false);
	        /*
	        URL imageURL = LineChartDemo1.class.getClassLoader().getResource(
	            "OnBridge11small.png");
	        if (imageURL != null) {
	            ImageIcon temp = new ImageIcon(imageURL);  
	            // use ImageIcon because it waits for the image to load...
	            chart.setBackgroundImage(temp.getImage()); 
	            plot.setBackgroundPaint(new Color(0, 0, 0, 0));            
	        }
	        */
	        // customise the range axis...
	        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
	        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

	        // customise the renderer...
	        LineAndShapeRenderer renderer 
	                = (LineAndShapeRenderer) plot.getRenderer();
	        renderer.setShapesVisible(true);
	        renderer.setDrawOutlines(true);
	        renderer.setUseFillPaint(true);
	        renderer.setFillPaint(Color.white);
	        renderer.setSeriesStroke(0, new BasicStroke(3.0f));
	        renderer.setSeriesOutlineStroke(0, new BasicStroke(2.0f));
	        renderer.setSeriesShape(0, new Ellipse2D.Double(-5.0, -5.0, 10.0, 10.0));
	        return chart;
	    }
	    
	    /**
	     * Creates a panel for the demo (used by SuperDemo.java).
	     * 
	     * @return A panel.
	     */
	    
	    public static JPanel createDemoPanel() {
	        JFreeChart chart = createChart(createDataset());
	        return new ChartPanel(chart);
	    }
	    
	    /**
	     * Starting point for the demonstration application.
	     *
	     * @param args  ignored.
	     */
	    /*
	    public static void main(String[] args) {
	        LineChartDemo1 demo = new LineChartDemo1("JFreeChart - Line Chart Demo 1");
	        demo.pack();
	        RefineryUtilities.centerFrameOnScreen(demo);
	        demo.setVisible(true);
	    }
		*/
	
}
